﻿using UnityEngine;
using Mirror;
using TMPro;
using System.Collections.Generic;
using System;

namespace Code.CharacterSystem
{

    public class CharacterSelect:NetworkBehaviour
    {
        [SerializeField]private GameObject _characterSelectDisplay = default;
        [SerializeField]private Transform _characterPreviewParent = default;
        [SerializeField]private TMP_Text _characterNameText = default;
        [SerializeField] private TMP_InputField _playerNameText = default;

        [SerializeField] private float _turnSpeed = 90f;
        [SerializeField] private CharacterConfig[] _characters = default;

        private int _currentCharacterIndex = 0;
        private List<GameObject> _characterInstances = new List<GameObject>();

        public NetworkBehaviour _currentNetPlayer;

        public Action<GameObject> OnPlayerReadyEvent;

        [SerializeField] public TMP_Text _readyButtonText;
        private bool _isReady;
        public override void OnStartClient()
        {
            //UIManager.Instance.ShowDieTitle(false);

            if (_characterPreviewParent.childCount == 0)
            {
                foreach (var character in _characters)
                {
                    GameObject characterInstance = Instantiate(character.CharacterPreviewPrefab, _characterPreviewParent);
                    characterInstance.SetActive(false);
                    _characterInstances.Add(characterInstance);
                }
            }
            _characterInstances[_currentCharacterIndex].SetActive(true);
            _characterNameText.text = _characters[_currentCharacterIndex].CharacterName;
            _characterSelectDisplay.SetActive(true);
        }
        
        private void Update()
        {
            _characterPreviewParent.RotateAround(
                _characterPreviewParent.position,
                _characterPreviewParent.up,
                _turnSpeed*Time.deltaTime);
        }

        public void Select()
        {
           
            _characterSelectDisplay.SetActive(false);
            CmdSelect(_currentCharacterIndex, _playerNameText.text,netIdentity.connectionToClient);
            
        }

        public void Ready() 
        {
            OnPlayerReadyEvent?.Invoke(_characters[_currentCharacterIndex].GameplayCharacterPrefab);
        }

        [Command(requiresAuthority =false)]
        private void CmdSelect(int characterIndex,string playerName,NetworkConnectionToClient sender=null)
        {
            _characterSelectDisplay.SetActive(false);
            GameObject characterInstance = Instantiate(_characters[characterIndex].GameplayCharacterPrefab);
            NetworkServer.ReplacePlayerForConnection(sender,characterInstance,true);
           
        }

        public void Right() 
        {
            _characterInstances[_currentCharacterIndex].SetActive(false);
            _currentCharacterIndex = (_currentCharacterIndex + 1) % _characterInstances.Count;
            _characterInstances[_currentCharacterIndex].SetActive(true);
            _characterNameText.text = _characters[_currentCharacterIndex].CharacterName;
        }
        public void Left() 
        {
            _characterInstances[_currentCharacterIndex].SetActive(false);
            _currentCharacterIndex--;
            if (_currentCharacterIndex < 0) 
            {
                _currentCharacterIndex += _characterInstances.Count;
            }
            _characterInstances[_currentCharacterIndex].SetActive(true);
        }

        
    }
}
